package com.duowei.diandan.adapter;

import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.Payment;

import java.util.ArrayList;
import java.util.List;

/**
 * 付款明细
 */
public class TicketSelectedAdapter extends RecyclerView.Adapter<TicketSelectedAdapter.ViewHolder> {

    private List<Payment> mList = new ArrayList<>();

    private RecyclerView mRecyclerView;
    private OnDataChangedListener mOnDataChangedListener;

    public TicketSelectedAdapter(RecyclerView recyclerView, OnDataChangedListener onDataChangedListener) {
        mRecyclerView = recyclerView;
        mOnDataChangedListener = onDataChangedListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.recycler_item_ticket_selected, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Payment item = mList.get(position);

        holder.titleView.setText(item.title);

        String moneyViewText = "¥" + Float.toString(item.payMoney).replaceAll("\\.?0*$", "");
        if (item.totalMoney != -1 && item.totalMoney != item.payMoney) {
            moneyViewText = moneyViewText + " (总面额¥" + Float.toString(item.totalMoney).replaceAll("\\.?0*$", "") + ")";
        }
        holder.moneyView.setText(moneyViewText);

        holder.numView.setText(String.valueOf(item.ticketNum));
        holder.deleteView.setOnClickListener(v -> {
            mList.remove(item);
            notifyDataSetChanged();

            if (mOnDataChangedListener != null) {
                mOnDataChangedListener.onDataChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public Payment getItem(int position) {
        return mList.get(position);
    }

    public void add(Payment item) {
        mList.add(item);
        notifyDataSetChanged();

        mRecyclerView.smoothScrollToPosition(mList.size() - 1);

        if (mOnDataChangedListener != null) {
            mOnDataChangedListener.onDataChanged();
        }
    }

    public List<Payment> getSelectedList() {
        return mList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleView;
        TextView moneyView;
        TextView numView;
        ImageView deleteView;

        public ViewHolder(View itemView) {
            super(itemView);
            titleView = itemView.findViewById(R.id.titleView);
            moneyView = itemView.findViewById(R.id.moneyView);
            numView = itemView.findViewById(R.id.numView);
            deleteView = itemView.findViewById(R.id.deleteView);
        }
    }

    public interface OnDataChangedListener {
        void onDataChanged();
    }
}
